﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using mars_server.csharp.model;
using mars_server.csharp.dao;
using mars_server.csharp.dao.impl;
using mars_server.csharp.VO;
using mars_server.csharp.enums;

namespace mars_server.csharp.AO.impl
{
    public class UserAOImpl : UserAO
    {
        //单例模式
        private UserAOImpl() { }
        private static readonly UserAOImpl instance = new UserAOImpl();
        public static UserAOImpl Instance
        {
            get
            {
                return instance;
            }
        }

        //单例对象
        RelationAO relationAO = RelationAOImpl.Instance;

        UserManager userManager = UserManagerImpl.Instance;

        UserSocketAO userSocketAO = UserSocketAOImpl.Instance;

        UserSocketManager userSocketManager = UserSocketManagerImpl.Instance;

        /**
         * 验证用户登录是否合法
         * */
        public UserDO verifyUserLogin(LoginVO loginVO)
        {
            if(loginVO == null || loginVO.username == null || loginVO.password == null)
            {
                return null;
            }
            Dictionary<string, string> sqlMap = new Dictionary<string, string>();
            sqlMap.Add("username", loginVO.username);
            List<UserDO> userDOs = userManager.selectUserDOBySingleMap(sqlMap);
            if (userDOs.Count == 0)
            {
                return null;
            }

            if (userDOs[0].password.Equals(loginVO.password)) 
            {
                return userDOs[0];
            }
            else
            {
                return null;
            }

        }

        public List<UserVO> queryUserVOsByUserVO(UserVO userVO)
        {
            List<UserVO> userVOs = new List<UserVO>();
            if (userVO == null || userVO.nickName == null)
            {
                return userVOs;
            }
            Dictionary<string, string> sqlMap = new Dictionary<string, string>();
            sqlMap.Add("nick_name", userVO.nickName);
            List<UserDO> userDOs = userManager.selectUserDOBySingleMapRegexp(sqlMap);
            foreach(UserDO userDO in userDOs)
            {
                foreach(UserDO onlineUserDO in userSocketAO.queryOnlineUserDO())
                {

                    if(onlineUserDO == null)
                    {
                        continue;
                    }
                    if(userDO.id == onlineUserDO.id)
                    {
                        UserVO tUserVO = new UserVO();
                        tUserVO.id = userDO.id;
                        tUserVO.nickName = userDO.nickName;
                        tUserVO.userIntro = userDO.userIntro;
                        tUserVO.headImg = userDO.headImg;
                        tUserVO.sex = userDO.sex;
                        tUserVO.isOnline = (int)IsOnlineEnum.online.Online;
                        userVOs.Add(tUserVO);
                    }
                }
            }
            return userVOs;
        }
    }
}
